package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.newJz.JCZQ_BQC_Fragment;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCZQResponseInfo;
import com.fyzk.net.Constants;
import com.fyzk.utils.AppUtil;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by miao on 2017/7/15.
 */

public class JCZQ_BQC_Adapter extends BaseExpandableListAdapter {


    private Context context;
    private ArrayList<JCZQResponseInfo> mList;
    //    private List<String> betcodes;
    private boolean isDG;
    private int count;
    private JCZQ_BQC_Fragment jczq_zjq_fragment;

    public JCZQ_BQC_Adapter(Context context, JCZQ_BQC_Fragment
            jczq_zjq_fragment, ArrayList<JCZQResponseInfo> infos) {
        this.context = context;
        this.mList = infos;
        this.jczq_zjq_fragment = jczq_zjq_fragment;
        if (App.isBaskOrder) {
            maxCount = 8;
        } else {
            maxCount = 10;
        }
    }

    public void setInfos(List<JCZQResponseInfo> infos, int i) {

    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setDG(boolean DG) {
        this.isDG = DG;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return this.mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.mList.get(groupPosition).matchDatas.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.mList.get(groupPosition).matchDatas.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition * 100 + childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        JCZQResponseInfo info = mList.get(groupPosition);
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.jclq_adapter_group, null);
            groupHolder = new GroupHolder();
            groupHolder.date_tv = (TextView) convertView
                    .findViewById(R.id.jclq_group_tv);
            groupHolder.jianTou_iv = (ImageView) convertView
                    .findViewById(R.id.jclq_group_iv);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        groupHolder.date_tv.setText(info.matchTime + "   " + info.matchWeekday
                + "    共 " + info.matchDatas.size() + " 场");
        if (isExpanded) {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_shun));
        } else {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_ni));
        }

        return convertView;
    }

    class GroupHolder {
        TextView date_tv;
        ImageView jianTou_iv;
    }

    private int maxCount = 10;
    private int index;

    private float TeamNameTvDefaultTSize = 0;//赛事主客队默认的字体大小

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        index = childPosition;
        final JCInfo infor = mList.get(groupPosition).matchDatas.get(childPosition);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.jz_team_item_bqc,
                    null);
            holder = new ViewHolder();

            holder.xianShi = (LinearLayout) convertView.findViewById(R.id
                    .jc_main_list_item_xianshi);
            holder.ll_xiangqing = (LinearLayout) convertView.findViewById(R.id
                    .jc_main_list_item_yincang);

            holder.ivIsDanGuan = (ImageView) convertView.findViewById(R.id
                    .ivIsDanGuan);

            holder.team = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_team);
            holder.num = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_num);
            holder.home = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_team_name1);
            holder.away = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_team_name2);
            TeamNameTvDefaultTSize = holder.away.getTextSize() / TypedValue.COMPLEX_UNIT_SP;
            holder.timeEnd = (TextView) convertView
                    .findViewById(R.id.jc_main_list_item_text_time_end);
            holder.bqc_ss = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_ss);
            holder.bqc_sp = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_sp);
            holder.bqc_sf = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_sf);
            holder.bqc_ps = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_ps);
            holder.bqc_pp = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_pp);
            holder.bqc_pf = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_pf);
            holder.bqc_fs = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_fs);
            holder.bqc_fp = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_fp);
            holder.bqc_ff = (CheckBox) convertView
                    .findViewById(R.id.bqc_team_item_ff);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.team.setBackgroundResource(AppUtil.getTeamNameColor(infor.getMatchName()));
        holder.team.setText(infor.getMatchName());

        holder.num.setText(infor.getMatchNum().substring(infor.getMatchNum().length() - 3) + "");

        holder.home.setText(infor.getHomeTeam());
        holder.away.setText(infor.getAwayTeam());


        JCInfo.SPInfo SpSS = infor.btnHGPojo.get(infor.BQC_33);
        JCInfo.SPInfo SpSP = infor.btnHGPojo.get(infor.BQC_31);
        JCInfo.SPInfo SpSF = infor.btnHGPojo.get(infor.BQC_30);
        JCInfo.SPInfo SpPS = infor.btnHGPojo.get(infor.BQC_13);
        JCInfo.SPInfo SpPP = infor.btnHGPojo.get(infor.BQC_11);
        JCInfo.SPInfo SpPF = infor.btnHGPojo.get(infor.BQC_10);
        JCInfo.SPInfo SpFS = infor.btnHGPojo.get(infor.BQC_03);
        JCInfo.SPInfo SpFP = infor.btnHGPojo.get(infor.BQC_01);
        JCInfo.SPInfo SpFF = infor.btnHGPojo.get(infor.BQC_00);


        holder.bqc_ss.setText(getShowSP("胜胜 " + SpSS.spValue));
        holder.bqc_sp.setText(getShowSP("胜平 " + SpSP.spValue));
        holder.bqc_sf.setText(getShowSP("胜负 " + SpSF.spValue));
        holder.bqc_ps.setText(getShowSP("平胜 " + SpPS.spValue));
        holder.bqc_pp.setText(getShowSP("平平 " + SpPP.spValue));
        holder.bqc_pf.setText(getShowSP("平负 " + SpPF.spValue));
        holder.bqc_fs.setText(getShowSP("负胜 " + SpFS.spValue));
        holder.bqc_fp.setText(getShowSP("负平 " + SpFP.spValue));
        holder.bqc_ff.setText(getShowSP("负负 " + SpFF.spValue));

        // 判断是否开售(赔率为空,禁止点击)
        holder.bqc_ss.setChecked(SpSS.isSel);
        holder.bqc_sp.setChecked(SpSP.isSel);
        holder.bqc_sf.setChecked(SpSF.isSel);
        holder.bqc_ps.setChecked(SpPS.isSel);
        holder.bqc_pp.setChecked(SpPP.isSel);
        holder.bqc_pf.setChecked(SpPF.isSel);
        holder.bqc_fs.setChecked(SpFS.isSel);
        holder.bqc_fp.setChecked(SpFP.isSel);
        holder.bqc_ff.setChecked(SpFF.isSel);
        SimpleDateFormat format_ = new SimpleDateFormat("HH:mm");
        holder.timeEnd.setText(format_.format(new Date(infor.getEndSaleTime())) + "[截]");

        onclikBtn();

        holder.bqc_ss.setTag(SpSS);
        holder.bqc_sp.setTag(SpSP);
        holder.bqc_sf.setTag(SpSF);
        holder.bqc_ps.setTag(SpPS);
        holder.bqc_pp.setTag(SpPP);
        holder.bqc_pf.setTag(SpPF);
        holder.bqc_fs.setTag(SpFS);
        holder.bqc_fp.setTag(SpFP);
        holder.bqc_ff.setTag(SpFF);

        holder.bqc_ss.setTag(R.string.jc_info, infor);
        holder.bqc_sp.setTag(R.string.jc_info, infor);
        holder.bqc_sf.setTag(R.string.jc_info, infor);
        holder.bqc_ps.setTag(R.string.jc_info, infor);
        holder.bqc_pp.setTag(R.string.jc_info, infor);
        holder.bqc_pf.setTag(R.string.jc_info, infor);
        holder.bqc_fs.setTag(R.string.jc_info, infor);
        holder.bqc_fp.setTag(R.string.jc_info, infor);
        holder.bqc_ff.setTag(R.string.jc_info, infor);


        holder.bqc_ss.setOnClickListener(scoreClickListener);
        holder.bqc_sp.setOnClickListener(scoreClickListener);
        holder.bqc_sf.setOnClickListener(scoreClickListener);
        holder.bqc_ps.setOnClickListener(scoreClickListener);
        holder.bqc_pp.setOnClickListener(scoreClickListener);
        holder.bqc_pf.setOnClickListener(scoreClickListener);
        holder.bqc_fs.setOnClickListener(scoreClickListener);
        holder.bqc_fp.setOnClickListener(scoreClickListener);
        holder.bqc_ff.setOnClickListener(scoreClickListener);

        if (infor.isShowHideView) {
            holder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            holder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (infor.hideView != null) {

            ViewGroup parent1 = (ViewGroup) infor.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            holder.ll_xiangqing.removeAllViews();
            holder.ll_xiangqing.addView(infor.hideView);
        }

        // 点击显示隐藏信息
        holder.xianShi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (infor.hideView == null) {
//                    infor.hideView = new FootballMatchSummaryView(context, infor.getMatchNum());
//                    holder.ll_xiangqing.removeAllViews();
//                    holder.ll_xiangqing.addView(infor.hideView);
//                }
//                infor.isShowHideView = !infor.isShowHideView;
//                if (infor.isShowHideView) {
//                    holder.ll_xiangqing.setVisibility(View.VISIBLE);
//                } else {
//                    holder.ll_xiangqing.setVisibility(View.GONE);
//                }
                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", true);
                intent.putExtra("matchNum", infor.getMatchNum());
                context.startActivity(intent);
            }

        });

//        holder.ll_xiangqing.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                // onClick1(JCZQ_BF_Activity.WHAT_FENXI, position);
//                Intent intent = new Intent(context, ScoreDetailActivity.class);
//                intent.putExtra("matchNum", infor.getMatchNum());
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }
    private SpannableString getShowSP(String CONTENT) {
        RelativeSizeSpan span = new RelativeSizeSpan(Constants.scaleJCTxt);
        SpannableString spannableString = new SpannableString(CONTENT);
        spannableString.setSpan(span, 0, 2, Spanned
                .SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }


    View.OnClickListener scoreClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onScoreClick(v);
        }
    };

    private void onScoreClick(View view) {
        JCInfo.SPInfo spInfo = (JCInfo.SPInfo) view.getTag();
        JCInfo infor = (JCInfo) view.getTag(R.string.jc_info);
        if (infor.getOnclikNum() == 0 && count == maxCount) {
            AppToast.getToast().show("最多选择" + maxCount + "场比赛");
            spInfo.isSel = (false);
            JCZQ_BQC_Adapter.this.notifyDataSetChanged();
            return;
        } else {
            boolean isFail = spInfo.isSel;
            spInfo.isSel = !isFail;
            if (spInfo.isSel == true) {
                infor.setOnclikNum(infor.getOnclikNum() + 1);
            } else {
                infor.setOnclikNum(infor.getOnclikNum() - 1);
            }
            onclikBtn();
        }
    }


    class ViewHolder {
        LinearLayout xianShi;
        LinearLayout ll_xiangqing;

        ImageView ivIsDanGuan;

        TextView team;
        TextView num;
        TextView home;
        TextView away;
        TextView timeEnd;

        CheckBox bqc_ss;
        CheckBox bqc_sp;
        CheckBox bqc_sf;
        CheckBox bqc_ps;
        CheckBox bqc_pp;
        CheckBox bqc_pf;
        CheckBox bqc_fs;
        CheckBox bqc_fp;
        CheckBox bqc_ff;
    }

    public void onclikBtn() {
        jczq_zjq_fragment.adapterChange(mList);
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}